
/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"771E8F3B-117A-4595-B144-9E436CDA4103"}
 */
function onShow(firstShow, event) {

	//CARGAR PASAJEROS
	var $sql = 'SELECT rp_reserva_pasaj_id, rp_apellido, rp_nombre FROM reservapsj WHERE rp_reserva_cab_id = ? AND rp_reserva_pasaj_id NOT IN (SELECT hp_reserva_pasaj_id FROM roomingpsj JOIN roominghab ON rg_hab_id = hp_hab_id WHERE rg_reserva_hotel_id = ?)'
	var $args = [rh_reserva_cab_id, rh_reserva_hotel_id]
	var $ds = databaseManager.getDataSetByQuery('sytek',$sql,$args,-1)
	
	elements.tab_psj.removeAllTabs()
	var $frm = solutionModel.getForm('rooming_list_psj')
	$frm.dataSource = $ds.createDataSource('ROOMING_PSJ_MEM')
	forms.rooming_list_psj.controller.recreateUI()
	elements.tab_psj.addTab('rooming_list_psj')
	
	if (reservahtl_to_roominghab.getSize() == 0)
	{
		for (var i = 1 ; i <= reservahtl_to_reservahab.getSize() ; i++)
		{
			var $hab = reservahtl_to_reservahab.getRecord(i)
	
			for (var j = 1 ; j <= $hab.rb_qty_habit ; j++)
			{
				reservahtl_to_roominghab.newRecord()
				reservahtl_to_roominghab.rg_hotelhab_th_com = utils.stringTrim($hab.reservahab_to_hotelhab.hotelhab_th_com) + ' ' + j
			}
		}
	}
	cargarTree()
}

/**
 * @properties={typeid:24,uuid:"428EEDC1-3844-46AB-8149-C650262E8A28"}
 */
function cargarTree()
{
	forms.rooming_arbol.elements.arbol.removeAllRoots()
	var bind1 = forms.rooming_arbol.elements.arbol.createBinding('sytek','roominghab')
	bind1.setNRelationName('roominghab_to_roomingpsj') 
	bind1.setTextDataprovider('rg_hotelhab_th_com')

	var bind2 = forms.rooming_arbol.elements.arbol.createBinding('sytek','roomingpsj')
	bind2.setTextDataprovider('nombre')
	
	forms.rooming_arbol.elements.arbol.addRoots(reservahtl_to_roominghab)
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"E2A5E3F2-893F-411C-B7C1-BA1E46A2C341"}
 */
function pasarPasajero(event) {
	var $camino = forms.rooming_arbol.elements.arbol.selectionPath
	if ($camino.length == 1)
	{
		var $clave = $camino[0] // 1 es la segunda posicion, arranca en 0
		reservahtl_to_roominghab.selectRecord($clave)
		
		reservahtl_to_roominghab.roominghab_to_roomingpsj.newRecord()
		reservahtl_to_roominghab.roominghab_to_roomingpsj.hp_reserva_pasaj_id = forms.rooming_list_psj.rp_reserva_pasaj_id
		forms.rooming_list_psj.controller.deleteRecord()

		cargarTree()
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"F1B8B526-3EDB-4321-86C3-B18D964BD74F"}
 */
function borrarPasajero(event) {
	var $camino = forms.rooming_arbol.elements.arbol.selectionPath
	if ($camino.length == 2)
	{
		var $clave = $camino[1] // 1 es la segunda posicion, arranca en 0
		/** @type {JSFoundset<db:/sytek/roomingpsj>}*/
		var $roomingPsj = databaseManager.getFoundSet('sytek','roomingpsj')
		$roomingPsj.loadRecords($clave)

		forms.rooming_list_psj.controller.newRecord()
		var $psj = $roomingPsj.roomingpsj_to_reservapsj
		forms.rooming_list_psj.rp_reserva_pasaj_id 	= $psj.rp_reserva_pasaj_id
		forms.rooming_list_psj.rp_apellido			= $psj.rp_apellido
		forms.rooming_list_psj.rp_nombre			= $psj.rp_nombre
		
		$roomingPsj.deleteRecord()
		cargarTree()
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"7F8D81F5-929E-4D11-987E-E842527A450F"}
 */
function IrACargaReserva(event) {
	forms.reserva_modificacion.controller.show()
}
